<?php
namespace App\Http\Controllers;
/**
 * 
 * 产品控制器
 * 
 * @author maqu
 *
 */
use App\Services\BaiduMapService;
use App\Services\GoodsService;
use App\Services\LogisticsService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class GoodsController extends BaseController {

    /**
     *
     * 商品详情
     *
     * @param $goods_id 商品ID
     * @return mixed
     */
    public function getDetail($goods_id){

        //参数编辑
        $goods_id = intval($goods_id);
        $user_id = intval(Input::get('user_id',0));
        $province = Input::get('province');
        if (!$goods_id) {
            return $this->args_invalid();
        }


        //main
        $service = new GoodsService();
        $comments = $service->getGoodsComments($goods_id,0,1);
        if(count($comments)>0){
            foreach($comments as $k=>$item){
                $comments[$k]['order']['evaluation_time'] = local_date_ymd($item['order']['evaluation_time']);
                if($item['evaluation_image'] && count($item['evaluation_image'])>0){
                    $images = json_decode($item['evaluation_image'],true);
                }else{
                    $comments[$k]['images'] = '';
                }
                if (is_array($images)) 
                {
                    foreach($images as $image){
                        $comments[$k]["images"][] = complete_url($image);
                    }
                }
                else
                {
                    $comments[$k]["images"] = '';
                }
                
            }
            unset($comments[$k]['evaluation_image']);
        }

        $data = $service->getGoodsDetail($goods_id,$user_id,true,true);

        if($data===false){
            return $this->failure('该商品不存在');
        }

        if($comments){
            $data['comments'] = $comments;
        }else{
            $data['comments'] = '';
        }
        $logistics = new LogisticsService();
        if($province){
            $region_id = DB::table('region')->where('region_name',$province)->value('region_id');
            $postage = $logistics->getPostage($goods_id,$region_id);
            $data['region'] = $province ;
            $data['postage'] = $postage;
        }
        /* 更新浏览次数 */
        $this->_update_views($goods_id);

        //编辑结果
        return $this->success($data);
    }

    /**
     *
     * @param recom_id
     * @param firstrow
     * @param pagesize
     *
     * @return json
     */
    public function recom_goods(){

        $firstrow = Input::get('firstrow',0);
        $pagesize = Input::get('pagesize',10);
        $recom_id =Input::get('recom_id',0);

        if(!$recom_id){
            return $this->args_invalid();
        }

        //推荐
        $goodsService= new GoodsService();
        $goods =$goodsService->getRecommendGoodsByRecomId($recom_id,true,false,$firstrow,$pagesize);

        return $this->success($goods);

    }

    /**
     *
     * @param $goods_id
     *
     * @return mixed
     *
     */
    public function getComments($goods_id){
        //参数编辑
        $goods_id = intval($goods_id);

        $firstrow = Input::get('firstrow',0);
        $pagesize = Input::get('pagesize',10);
        if (!$goods_id) {
            return $this->args_invalid();
        }

        $service = new GoodsService();

        $data = $service->getGoodsComments($goods_id,$firstrow,$pagesize);

        if(count($data)>0){
            foreach($data as $k=>$item){
                $data[$k]['order']['evaluation_time'] = local_date_ymd($item['order']['evaluation_time']);
                if($item['evaluation_image'] && count($item['evaluation_image'])>0){
                    $images = json_decode($item['evaluation_image'],true);
                }else{
                    $data[$k]['images'] = '';
                }
                if (is_array($images) && count($images)>0)
                {
                    foreach($images as $image){
                        $data[$k]["images"][] = complete_url($image);
                    }
                }
                else
                {
                    $data[$k]['images'] = '';
                }
                unset($data[$k]['evaluation_image']);
            }
        }
        //编辑结果
        return $this->success($data);


    }

    /**
     * 获取商品列表
     *
     * @param GoodsService $goodsService 商品服务
     * @param BaiduMapService $mapService 地图服务服务
     * @return mixed
     */
    public function getGoodsList(GoodsService $goodsService,BaiduMapService $mapService){

        //参数
        //【经纬度】
        //    lat   经度
        //    lon   维度
        //【查询等级】
        //    level city or country
        //【排序】
        //  sort_type 排序方式 price or sales or distance or views
        //  sort    升降序 asc or desc
        //【位置】
        // country 0-全部 or 选择的县名
        //【翻页】
        //firsstrow
        //pagesize

        $cate_id =Input::get('cate_id',0);
        $lat =Input::get('lat',999);
        $lon =Input::get('lon',999);
//        $level =Input::get('level','city');
//		$cityname = Input::get('cityname');
        $city = Input::get('city');
        $province = Input::get('province');
        $country =Input::get('district');
        if(empty($country)){
            $country =Input::get('country');
        }
        $sort_type =Input::get('sort_type','price');
        $sort =Input::get('sort','asc');
        $firstrow =Input::get('firstrow',0);
        $pagesize =Input::get('pagesize',10);

		if($lat=='undefined'){
			$lat = 999;
		}
		if($lon=='undefined'){
			$lon = 999;
		}
		
//        if(!$cate_id){
//            return $this->args_invalid();
//        }
//        $location = $mapService->locationByGPS($lon,$lat);
//        if(!$location){
//            return $this->failure('根据定位获取城市失败。');
//        }
//        $data = $goodsService->getList($cate_id,$lat,$lon,$level,$location['province'],$location['city'],$country,$sort_type,$sort,$firstrow,$pagesize);

        $data = $goodsService->getList($cate_id,$lat,$lon,$province,$city,$country,$sort_type,$sort,$firstrow,$pagesize);
        if(!$data['result']){
            return $this->failure($data['msg']);
        }
        //编辑结果
        foreach ($data['data']['goods'] as $item){
            if(!empty($item->default_image)){
                $item->default_image = complete_url($item->default_image);
            }
        }
        return $this->success($data['data']);
    }

    /* 更新浏览次数 */
    private function _update_views($id){
        DB::table('goods_statistics')->where('goods_id',$id)->increment('views');
    }

}
